<?php

include_once 'Super_Model_Admin.php';

class GetSnippets extends Super_Model_Admin {

    static function getCategoriesSelect($parent_id = 0) {
        $temp = array();
        $model = new Admin_Model_Cats();
        $temp = $model->getCats();
        $result = '';
        if (is_array($temp) && count($temp)) {
            $result = '<select name="category_id" id="category_id" class="send-val" style="width: 210px;">';
            $result .= '<option value="0"></option>';
            foreach ($temp as $val) {
                $select = (!empty($parent_id) && $parent_id == $val['id']) ? ' selected ' : '';
                if (empty($val['child']))
                    $result .= '<option ' . $select . ' value="' . $val['id'] . '">' . $val['title'] . '</option>';

                else {
                    $result .= '<option ' . $select . ' value="' . $val['id'] . '">' . $val['title'] . '</option>';
                    foreach ($val['child'] as $child) {
                        $result .= '<option value="' . $child['id'] . '">&nbsp;&nbsp;&nbsp;&nbsp;' . $child['title'] . '</option>';
                    }
                }
            }
            $result .= '</select>';
        }
        return $result;
    }

    static function getShortStoriesCategoriesSelect($item_val = 0) {
        $model = new Application_Model_ShortStories();
        $temp = $model->getShortStoriesCat();
        if (is_array($temp) && count($temp)) {
            $result = '<select name="category_id" id="category_id" class="send-val" style="width: 210px;">';
            $result .= '<option value="0"></option>';
            foreach ($temp as $key => $val) {
                $select = (!empty($item_val) && $item_val == $key) ? ' selected ' : '';
                $result .= '<option ' . $select . ' value="' . $key . '">' . $val . '</option>';
            }
            $result .= '</select>';
        }
        return $result;
    }

    /*
     *  Generate input field or other HTML code
     *  @param array with parameter of the field
     */

    static function getField(array $val) {

        $it = '';
        $valid = (isset($val['valid'])) ? ' valid' : '';
        $name = (isset($val['name'])) ? $val['name'] : $val['name'];
        $id = (isset($val['id'])) ? $val['id'] : $val['name'];
        $title = (isset($val['title'])) ? $val['title'] : $val['name'];
        $js_data = (isset($val['js-data'])) ? ' js-data' : '';
        $email = (isset($val['email'])) ? ' email' : '';
        $class = (isset($val['class'])) ? ' ' . $val['class'] : '';
        $value = (isset($val['value'])) ? $val['value'] : '';
        $style = (isset($val['style'])) ? ' style="' . $val['style'] . '"' : '';
        $reverse = (isset($val['reverse'])) ? true : false;
        $readonly = (isset($val['readonly'])) ? 'readonly' : '';

        switch ($val['type']) {
            case 'str':
                $it = '<input type="text" ' . $readonly . ' value="' . $value . '" name="' . $name . '" id="' . $id . '" ' . $style . ' class="inp-txt ' . $valid . $js_data . $email . $class . '" />';
                break;

            case 'text':
                $it = '<textarea name="' . $name . '" id="' . $id . '" ' . $style . ' class="inp-txt ' . $valid . $js_data . $email . $class . '">' . stripcslashes($value) . '</textarea>';
                break;

            case 'password':
                $it = '<input type="password" ' . $readonly . ' value="' . $value . '" name="' . $name . '" id="' . $id . '" ' . $style . ' class="inp-txt ' . $valid . $js_data . $email . $class . '" />';
                break;

            case 'build':
                $obj = new GetSnippets();
                if (method_exists($obj, $val['function'])) {
                    if (isset($value)) {
                        $it = call_user_func(array($obj, $val['function']), $value);
                    } else {
                        $it = call_user_func(array($obj, $val['function']));
                    }
                }
                break;

            case 'label':
                $it = '<span rel="' . $name . '" id="' . $id . '" class="' . $js_data . $class . '">' . $value . '</span>';
                break;

            case 'one-checkbox':
                $checked = ($value > 0) ? 'CHECKED' : '';
                if (!empty($reverse))
                    $checked = ($checked) ? '' : 'CHECKED';
                $it = '<input type="checkbox"  ' . $checked . ' value="1" name="' . $name . '" id="' . $id . '" ' . $style . ' class="' . $valid . $js_data . $email . $class . '" />';
                break;

            case 'date':
                $idDt = 'date-' . rand(100, 10000);
                if (empty($value))
                    $value = date('Y-m-d');

                $it = '<input type="text" value="' . $value . '" name="' . $name . '" id="' . $idDt . '" ' . $style . ' class="inp-txt inp-calendar height17 ' . $valid . $js_data . $email . $class . '" />';
                $it .= '<script language="JavaScript">
                            $( "#' . $idDt . '" ).datepicker({ dateFormat: "yy-mm-dd 00:00:00",changeMonth: true, changeYear: true });
                       </script>';
                break;

            default:
                $it = Zend_Registry::get('trans')->_($val['name']);
                break;
        }
        return $it;
    }

    static function getRoleSelect($select = '', $is_add_none_item = false) {
        $temp = array();
        $model = new Admin_Model_AdminGroups();
        $temp = $model->getRole();
        if ($temp) {
            $result = '<select name="role_id" style="width: 170px;">';
            if ($is_add_none_item)
                $result .= '<option>-- none --</option>';
            foreach ($temp as $val) {
                if (!$is_add_none_item || ($is_add_none_item && $val['id'] !== 1))
                    $result .= '<option  ' . (($select == $val['id']) ? 'selected' : '' ) . ' value="' . $val['id'] . '">' . $val['title'] . '</option>';
            }
            $result .= '</select>';
        }
        return $result;
    }

    static function getLnSelect($name = "", $select = '', $some = "style='width: 170px;'") {
        $temp = array();
        $model = new Admin_Model_Tariffs();
        $temp = $model->getLanguages();
        if ($temp) {
            $result = '<select class="send-val selectLN" name="' . $name . '" ' . $some . '>';
            foreach ($temp as $val) {
                $result .= '<option ' . (($select == $val['Abbreviation']) ? 'selected' : '' ) . ' value="' . $val['Abbreviation'] . '">' . $val['Title'] . '</option>';
            }
            $result .= '</select>';
        }
        return $result;
    }

    static function getYN($value = '') {
        $result = '<span>' . Zend_Registry::get('trans')->_('no_') . '</span>';
        if ($value)
            $result = '<span style="color: green">' . Zend_Registry::get('trans')->_('yes_') . '</span>';
        return $result;
    }

    static function getNY($value = '') {
        $result = '<span style="color: green">' . Zend_Registry::get('trans')->_('yes_') . '</span>';
        if ($value)
            $result = '<span>' . Zend_Registry::get('trans')->_('no_') . '</span>';
        return $result;
    }

    static function codeUrl($title) {
        return str_replace('/', '|', $_SERVER['REDIRECT_URL']) . $title;
    }

    static function getLabelsSelect($name = 'labels', $select = '', $some = "style='width: 170px;'") {
        $labels = array();
        $ALM = new Admin_Model_Labels();
        $labels = $ALM->getAllLabels();
        if ($labels) {
            $result = '<select id="labels" multiple="multiple" class="send-val-multi" name="labels" ' . $some . '>';
            foreach ($labels as $val) {
                $result .= '<option value="' . $val['id'] . '">' . $val['title'] . '</option>';
            }
            $result .= '</select>';
            return $result;
        }
        return false;
    }

    static function getShortStoriesSelect($select = '', $some = "style='width: 170px;'", $name = 'type', $bZeroItem = FALSE) {
        $SSTRM = new Application_Model_ShortStories();
        $sttype = $SSTRM->getShortStoriesTypes();
        if ($sttype) {
            $result = '<select id="type" class="send-val" name="' . $name . '" ' . $some . '>';
            if ($bZeroItem)
                $result .= '<option value=""></option>';
            foreach ($sttype as $val) {
                $result .= '<option value="' . $val['id'] . '">' . $val['title'] . '</option>';
            }
            $result .= '</select>';
            return $result;
        }
        return false;
    }

    function getPagesSelect($name = 'pages', $select = '', $some = "style='width: 270px;'") {
        $labels = array();
        $labels = $this->select('pages', '`id`,`title`', ' `parent_id` = 0 ', '`title`');
        if ($labels) {
            $result = '<select id="pages" class="send-val" name="pages" ' . $some . '>';
            $result .= '<option value=""></option>';
            foreach ($labels as $val) {
                $sel = ($select == $val['id']) ? 'selected' : '';
                $result .= '<option ' . $sel . ' value="' . $val['id'] . '">' . $val['title'] . '</option>';
            }
            $result .= '</select>';
            return $result;
        }
        return false;
    }

    static function deCodeUrl($strUrl) {
        if (isset($strUrl)) {
            $strUrl = str_replace('||', '|', $strUrl);
            $temp = explode('|', $strUrl);
            $title = array_pop($temp);
            return '<a class="backLink" href="' . (implode('/', $temp) . '/') . '">' . Zend_Registry::get('trans')->_($title) . '</a>';
        }
    }

    static function addJsVar() {
        $str = '<script type="text/javascript">
            redirectUrl = \'' . $_SERVER['REDIRECT_URL'] . '\';
            moduleName = \'' . Zend_Controller_Front::getInstance()->getRequest()->getControllerName() . '\';
        </script>';
        $message = MessBox::getMessages();
        MessBox::clearMessages();
        if (is_array($message)) {
            $mess = '';
            foreach ($message as $er) {
                $mess .= Zend_Registry::get('trans')->_($er) . '<br />';
            }
            if ($mess) {
                $str .= '<div id="messageBox" style="display:none;">' . $mess . '</div>';
            }
        }
        return $str;
    }

}